
import { Injectable } from '@angular/core';
import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { AssetService } from './AssetService';
import { Asset } from '../models/Asset';
import {environment} from '../environments/environment';

@Injectable()
export class AssetsResolveService implements Resolve<{total: number, assets: Asset[]}> {

    constructor(private assetService: AssetService) {}

    resolve(route: ActivatedRouteSnapshot): Observable<{total: number, assets: Asset[]}>  {
      const options: any = { assetName: null, assetUser: null, assetDepartmentId: null, assetType: null, orders: ['buyDate|desc']};
        return this.assetService.getAssets(1, environment.pageSize, options).pipe(map(res => {
          if (res.success) {
            return  {total: res.total, assets: res.res};
          } else {
            console.error(res.res);
            return {total: 0, assets: []};
          }

        }));
    }
}

